# libraries
require 'rubygems'
require 'appscript'
require 'singleton'
include Appscript

class QTHandler
  
  include Singleton
  
  attr_accessor :video
  
  def refresh
    qt = app('QuickTime Player')
    docs = qt.get(qt.documents)
    docs.each do |doc|
      path = qt.get(doc.path)
      current_time = qt.get(doc.current_time)
      duration = qt.get(doc.duration)
      playing = qt.get(doc.playing)
      
      if @video.nil? or path != @video.path
        @video = Video.new(path, current_time, duration)
      else
        @video.current_time = current_time
        @video.duration = duration
      end

      # # check if existing
      # new_video = true
      # @videos.each do |video| 
      #   if video.path == path 
      #     new_video=false
      #     video.duration = duration
      #     video.current_time = current_time
      #   end
      # end
      # 
      # if new_video
      #   video = Video.new(path, current_time, duration)
      #   @videos << video
      # end
    end
    puts "watching:" + @video.to_s
  end
end
  